<?php
include ("../main/inc/global.inc.php");

require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
require_once (api_get_path(CONFIGURATION_PATH).'profile.conf.php');

api_block_anonymous_users(); // only users who are logged in can proceed

// Top part of the HTML
Display :: display_header('File List');

global $_user;

?>
<?php
$result = Database::getFileList();
echo "<div id='UploadLeft'>";

echo "<table class='Upload' align='left'>
<tr>
<th>File Path</th>
<th>Upload By</th>
<th>Create Date and Time</th>
</tr>";

$fileIdCommentList = array();

foreach ($result as &$row)
{
    array_push($fileIdCommentList, $row);    
    echo "<tr>";
    echo "<td><a href='" . $row['path'] ."'>" . $row['path'] ."</a></td>";
    echo "<td>" . $row['username'] . "</td>";
    echo "<td>" . $row['createDateTime'] . "</td>";
    echo "</tr>";
}
echo "</table>";
echo "</div>";
//set isRead to True;
$resultIsRead = Database::updateFileList($_user["user_id"]);

echo "<div id='UploadRight'>";
echo "<ul class='Upload'>";
foreach ($fileIdCommentList as &$value) {
    echo "<li>";
    echo "<table class='Upload'>";
    $commentId = "comment" . $value['id'];
    $commentListId = "commentList" . $value['id'];
    $buttonId = "button" . $value['id'];
    //Check tag by
    $resultTag = Database::getFileCommentList($value["id"],$_user["user_id"]);
    $rowTag = $resultTag[0];
    $tagCount = $rowTag['count'];
    //echo "<p>$tagCount</p>";
    echo "<tr>";
    echo "<th width='250px'>" . $value['path'] . "</th>";
    echo "<th width='170px'></th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td id=$commentListId width='250px'>";
    include "LoadCommentList.php";
    echo "</td>";

    echo "<td width='170px';>";
    if ($tagCount > 0 or $value['uploadId'] == $_user['user_id'])
    {
        echo "<textarea id=$commentId rows='1' cols='20'>Leave your comment here.</textarea> " . "<input type=button name='comment' value='Comment' onclick='sendComment($value[id]);'/> ";
    }
    else
    {
        echo "You are not tagged.";
    }
    echo "</td>";

    echo "</tr>";
    echo "</table>";
    echo "</li>";
}
echo "</ul>";
unset($value);

echo "</div>";
?>

<?php
// Footer
Display :: display_footer();
?>

<script>
function sendComment(fileId)
{
var uploadId = <?php echo $_user['user_id'] ?>;
var xmlhttp;   
var commentId = "comment" + fileId;
var commentListId = "commentList" + fileId;
var comment = document.getElementById(commentId).value;
if (comment=="")
  {
  document.getElementById(commentId).value="Nothing!";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //document.getElementById("comment").innerHTML=xmlhttp.responseText;
    var res = xmlhttp.responseText;
    console.log(res);
    document.getElementById(commentId).value="Succeeded!";
    document.getElementById(commentListId).innerHTML=res;
    }
  }

var parameters="fileId="+fileId+"&uploadId="+uploadId+"&comment="+comment;
xmlhttp.open("POST", "SendComment.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameters);

}

</script>
